package controller.filemanager;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import model.filemanager.FileManager;


@SuppressWarnings("serial")
public class UploadFileServlet extends HttpServlet {

	@Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    		
    		UserService userService = UserServiceFactory.getUserService();
    		User user = userService.getCurrentUser();
    	    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    		FileManager fm = new FileManager(user.getEmail());
    		
    		Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);

    		BlobKey blobKey = blobs.get("files[]");
    		BlobInfoFactory factory = new BlobInfoFactory();
    		
    		if(blobKey == null)
    			throw new IOException("No File Specified");
    		
    		BlobInfo info = factory.loadBlobInfo(blobKey);
    		
    		String type = info.getContentType().substring(0, info.getContentType().indexOf('/'));
    		
    		if(!type.equals("image") && !type.equals("audio") && !type.equals("video")) {
    			blobstoreService.delete(blobKey);
    			throw new IOException("Invalid File Fomart");
    		}
    		
    		fm.SaveFile(blobKey, info.getFilename(),type, info.getSize());
    		

    		res.sendRedirect("/upload?key=" + blobKey.getKeyString());

    }
	
	@Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
		
			String key = req.getParameter("key");
			if (key != null)
			{
				BlobInfoFactory factory = new BlobInfoFactory();
				BlobKey blobKey = new BlobKey(key);
				BlobInfo info = factory.loadBlobInfo(blobKey);
	    		String response = "";
	    		response = response.concat(String.format("[{\"name\":\"%s\",", info.getFilename()));
	    		response = response.concat(String.format("\"size\":%d}]", info.getSize()));
	    		
	    		res.setHeader("Content-Type","application/json");
	    		res.getWriter().write(response);
			}
			else
			{			
			BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
			String url = blobstoreService.createUploadUrl("/upload");
			res.setHeader("Content-Type","application/json");
			res.getWriter().write("\"" + url + "\"");
			}
					

        }
}